﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Collections.ObjectModel;
using BAL.EstRec;
using DAL.EstRec;
using System.IO;

namespace BAL.EstRec
{
    public class metodosFDV
    {
        string version;

        public metodosFDV()
        {
        }

        public metodosFDV(string version)
        {
            this.version = version;            
        }
        
        public bool validarDatosFDV(string sPOS, string sRecursos, string sCantidad) {

            if (validaCantidad(sCantidad)) {

                if (validarPOS(sPOS)) {

                    if (validaRecurso(sRecursos))
                    {
                        if (validaRecPos(sPOS, sRecursos))
                        {
                            return true;
                        }
                        return false;
                    }
                    return false;
                }
                return false;
            }
            return false;
        }

        public void InsertMasivo(DataTable dt) {
            
            string query = "";
            int col = 0;

            foreach (DataRow dr in dt.Rows)
            {                
                query += "EXEC [SP_CATALOGOS] ";

                foreach (DataColumn dc in dt.Columns)
                {
                    if (col == 0)
                        query += " @IDPOS = '";
                    if (col == 1)
                        query += " , @IDRECURSO = '";
                    if (col == 2)
                        query += " , @CANTIDAD = ";

                    query += dr.ItemArray[col].ToString();
                    if(col != 2)
                        query += "'";
                    col += 1;
                }

                query += ", @PUBLISH = 1, @VERSION = "+ version +";\n";

                col = 0;
            }

            //Prueba para guardar los EXEC en un txt
            //StreamWriter sw = new StreamWriter("C:\\Users\\ivan\\Desktop\\pepito.txt", false, System.Text.Encoding.Default);
            //sw.WriteLine(query);
            //sw.Close();

            //Conexión a la base de datos
            SrvDB.ExecuteNonQuery(query);
        }

        public bool validaRecPos(string sPOS, string sRecursos){

            string query = "EXEC SP_VALIDA_REC_POS @POS ='" + sPOS + "' ,@RECURSO = '" + sRecursos + "', @VERSION = " + version + ";";
            
            //query += "DECLARE @POSS nvarchar(1024); ";
            //query += "DECLARE @RECURSOSS nvarchar(1024); ";

            //query += "SET @POSS = (SELECT p.ID_POS FROM POS AS p WHERE p.POS = '" + sPOS + "'); ";
            //query += "SET @RECURSOSS = (SELECT r.ID_RECURSO FROM RECURSO AS r WHERE r.RECURSO = '" + sRecursos + "'); ";

            //query += "SELECT * FROM FDV_BGT WHERE ID_POS = @POSS AND ID_RECURSO = @RECURSOSS AND UNID_FDV_BGT_VERSION = "+version+"; ";
                       
            DataTable dt = SrvDB.ExecuteQuery(query);
            if (dt.Rows.Count > 0)
            {

                MessageBoxResult result = MessageBox.Show("Ya existe una relación entre POS, Recurso y Version.");
                return false;
            }
            return true;
        }

        public bool validaRecPos2(string sPOS, string sRecursos, string ver)
        {

            string query = "EXEC SP_VALIDA_REC_POS @POS ='" + sPOS + "' ,@RECURSO = '" + sRecursos + "', @VERSION = " + ver + ";";
            
            DataTable dt = SrvDB.ExecuteQuery(query);
            if (dt.Rows.Count > 0)
            {
                 return false;
            }
            return true;
        }

        public bool validaRecPos2FDV(string sPOS, string sRecursos, string ver)
        {

            string query = "EXEC SP_VALIDA_REC_POS @POS ='" + sPOS + "' ,@RECURSO = '" + sRecursos + "', @VERSION = " + ver + ";";
            
            DataTable dt = SrvDB.ExecuteQuery(query);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            return true;
        }
        public bool validaRecPos2HQ(string sPOS, string sRecursos, string ver)
        {

            string query = "EXEC SP_VALIDA_REC_POS_HQ @POS ='" + sPOS + "' ,@RECURSO = '" + sRecursos + "', @VERSION = " + ver + ";";
            
            DataTable dt = SrvDB.ExecuteQuery(query);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            return true;
        }

        public bool validarPOS(string sPOS)
        {
            if (Pos.isPosOnCatalog(sPOS))
                return true;
            MessageBoxResult result = MessageBox.Show("Ingrese un nombre de POS que se encuentre en la base de datos.");
            return false;
        }

        public bool validarPOS2(string sPOS)
        {
            if (Pos.isPosOnCatalog(sPOS))
                return true;
            return false;
        }

        public bool validaRecurso(string sRecursos)
        {
            if(Recurso.isRecursoOnCatalog(sRecursos))
                return true;

            MessageBoxResult result = MessageBox.Show("Ingrese un nombre de recurso que se encuentre en la base de datos.");
            return false;
        }

        public bool validaRecurso2(string sRecursos)
        {
            if (Recurso.isRecursoOnCatalog(sRecursos))
                return true;
            return false;
        }

        private bool validaCantidad(string sCantidad)
        {
            try
            {
                int num = 0;
                num = Int16.Parse(sCantidad);
                return true;
            }
            catch (Exception Ex) {
                                
                return false;
            }
        }

        public DataTable datosConVersionHQ(string ver, string pos, string rec) {

            string query = "EXEC SP_DATOS_CON_VERSION_HQ @POS = '" + pos + "', @RECURSO = '" + rec + "', @VERSION = " + ver;

            DataTable dt = SrvDB.ExecuteQuery(query);
            return dt;
        }

        public DataTable datosConVersionFDV(string ver, string pos, string rec)
        {
            string query = "EXEC SP_DATOS_CON_VERSION_FDV @POS = '"+pos+"', @RECURSO = '"+rec+"', @VERSION = " + ver ;

            DataTable dt = SrvDB.ExecuteQuery(query);
            return dt;
        }

        public DataTable datosConVersion(string ver) {

            string query = "EXEC SP_DATOS_CON_VERSION @VERSION = " + ver;

            DataTable dt = SrvDB.ExecuteQuery(query);
            return dt;
        }
    }
}
